// by Xeno
//#define __DEBUG__
#define THIS_FILE "x_vrespawn2.sqf"
#include "x_setup.sqf"

#define __Trans(tkind) getNumber(configFile/#CfgVehicles/typeOf _vehicle/#tkind)
#define __TransCargo 0.15
private ["_vehicle", "_camotype", "_camo", "_i", "_disabled", "_empty", "_outb", "_hasbox", "_fuelleft","_reserved","_vec_varname"];
if (!isServer) exitWith{};

_vec_array = [];
{
	__TRACE_1("_x","_x");
	_vehicle = _x select 0;
	if (!isNil "_vehicle" && {!isNull _vehicle}) then {
		// TYRGHEN
		_reserved = false;
		if (count _x > 3) then { _reserved = _x select 3 };
		_vec_varname = vehicleVarName _vehicle;

		_number_v = _x select 1;
		_arr = [_vehicle,_number_v,position _vehicle,direction _vehicle,typeOf _vehicle,_reserved,_vec_varname];
		__TRACE_1("","_arr")
		_vec_array set [count _vec_array, _arr];
		
		_vehicle setVariable [QGVAR(OUT_OF_SPACE), -1];
		_vehicle setVariable [QGVAR(vec), _number_v, true];
		_vehicle setAmmoCargo 0;
		_vehicle setVariable [QGVAR(vec_islocked), (_vehicle call FUNC(isVecLocked))];

		// TYRGHEN ADDITION
		_vehicle setVariable [QGVAR(team_reserved),_reserved,true];
		if (_reserved) then {
			_vehicle addEventHandler ["Engine", {_this spawn {sleep 3; if (isnull (driver (_this select 0)) && isEngineOn(_this select 0)) then { (_this select 0) engineOn false; (_this select 0) setFuel 0; sleep 1; (_this select 0) setFuel 1; };}}];
		};
		_vehicle addEventHandler ["GetIn", {if (!isplayer (_this select 2) && !isplayer(leader (_this select 2))) then { (_this select 2) action ["eject",(_this select 0)]; }; }];
		// TYRGHEN
		_vehicle addItemCargoGlobal ["ToolKit", 2];

		#ifdef __TT__
		if (_number_v < 1000) then {
			_vehicle addMPEventhandler ["MPKilled", {if (isServer) then {_this call FUNC(checkveckillblufor)}}];
		} else {
			_vehicle addMPEventhandler ["MPKilled", {if (isServer) then {_this call FUNC(checkveckillopfor)}}];
		};
		#endif
		if (_number_v < 100 || {(_number_v > 999 && {_number_v < 1100})}) then {
			_vehicle addMPEventhandler ["MPKilled", {(_this select 0) call FUNC(MHQFunc)}];
		};
		_vehicle addMPEventhandler ["MPKilled", {if (isServer) then {_this call FUNC(fuelCheck)}}];
		
		if (__Trans(transportAmmo) > 0) then {
			_vehicle setAmmoCargo __TransCargo;
		} else {
			if (__Trans(transportRepair) > 0) then {
				_vehicle setRepairCargo __TransCargo;
			} else {
				if (__Trans(transportFuel) > 0) then {
					_vehicle setFuelCargo __TransCargo;
				};
			};
		};
		
		// TODO No camo nets in A3
		if (GVAR(with_base_camonet) == 0) then {
			_camotype = switch (getNumber (configFile/"CfgVehicles"/typeOf _vehicle/"side")) do {
				case 1: {"CamoNet_OPFOR_big_F"}; // Blufor Vec
				case 0: {"CamoNet_BLUFOR_big_F"};
			};
			_camo = createVehicle [_camotype, position _vehicle, [], 0, "NONE"];
			_camo setDir (direction _vehicle) + 180;
			_camo setPos (position _vehicle);
			_vehicle setVariable [QGVAR(camonet), _camo];
		};
	};
} forEach _this;
_this = nil;

sleep 11;

while {true} do {
	sleep 8 + random 5;
	call FUNC(mpcheck);
	{
		_vec_a = _x;
		_vehicle = _vec_a select 0;
		
		_disabled = (damage _vehicle >= 0.9);
		
		_empty = _vehicle call FUNC(GetVehicleEmpty);
		
		_aliveve = alive _vehicle;

		if (_empty && {!_disabled} && {_aliveve} && {(_vehicle call FUNC(OutOfBounds))}) then {
			_outb = GV(_vehicle,GVAR(OUT_OF_SPACE));
			if (_outb != -1) then {
				if (time > _outb) then {_disabled = true};
			} else {
				_vehicle setVariable [QGVAR(OUT_OF_SPACE), time + 600];
			};
		} else {
			_vehicle setVariable [QGVAR(OUT_OF_SPACE), -1];
		};
		
		sleep 0.01;

		if (_empty && {_disabled || {!_aliveve}}) then {
			_number_v = _vec_a select 1;
			_hasbox = GV(_vehicle,GVAR(ammobox));
			_fuelleft = _vehicle getVariable [QGVAR(fuel), 1];
			//TYRGHEN
			if (_fuelleft <= 0.3) then { _fuelleft = 1};
			if (!isNil "_hasbox" && {_hasbox}) then {[QGVAR(num_ammo_boxes),__XJIPGetVar(GVAR(num_ammo_boxes)) - 1] call FUNC(NetSetJIP)};
			if (_number_v < 100 || {(_number_v > 999 && {_number_v < 1100})}) then {
				_dhqcamo = GV(_vehicle,GVAR(MHQ_Camo));
				if (!isNil "_dhqcamo" && {!isNull _dhqcamo}) then {deleteVehicle _dhqcamo};
			};
			if (GVAR(with_base_camonet) == 0) then {
				_camo = _vehicle getVariable [QGVAR(camonet), objNull];
				if (!isNull _camo) then {deleteVehicle _camo;_camo = objNull};
			};
			_isitlocked = GV(_vehicle,GVAR(vec_islocked)); // || {_vehicle call FUNC(isVecLocked)};
			// TYRGHEN
			if (isnil "_isitlocked") then { _isitlocked = false };
			sleep 0.1;
			deleteVehicle _vehicle;
			sleep 0.5;
			// TYRGHEN - emptying the spot of respawn
			_objs = nearestObjects [_vec_a select 2, ["CAManBase","AllVehicles"], 5];
			__TRACE_1("","_objs")
			{
				_new_pos = (_vec_a select 2) findEmptyPosition[ 7 , 50, typeof _x];
				if (count _new_pos > 0) then { _x setPos _new_pos; sleep 1; };
			} foreach _objs;
			
			_vehicle = objNull;
			_vehicle = createVehicle [_vec_a select 4, _vec_a select 2, [], 0, "NONE"];
			_vehicle setdir (_vec_a select 3);
			_vehicle setpos (_vec_a select 2);
			_vehicle setFuel _fuelleft;
			
			// TYRGHEN - in cases of spawning over debris			
			_vehicle allowDamage false;
			_vehicle spawn { sleep 10; _this allowDamage true; };
			_vec_varname = (_vec_a select 6);
			_vehicle setVehicleVarName _vec_varname;
			_vehicle setVariable [QGVAR(vec_varname),_vec_varname,true];
			_vehicle call compile format ["%1=_vehicle;",_vec_varname];
			
			// TYRGHEN ADDITION
			_reserved = _vec_a select 5;
			_vehicle setVariable [QGVAR(team_reserved),_reserved,true];
			if (_reserved) then {
				_vehicle addEventHandler ["Engine", {_this spawn {sleep 3; if (isnull (driver (_this select 0)) && isEngineOn(_this select 0)) then { (_this select 0) engineOn false; (_this select 0) setFuel 0; sleep 1; (_this select 0) setFuel 1; };}}];
			};
			_vehicle addEventHandler ["GetIn", {if (!isplayer (_this select 2) && !isplayer(leader (_this select 2))) then { (_this select 2) action ["eject",(_this select 0)]; }; }];
			// TYRGHEN
			_vehicle addItemCargoGlobal ["ToolKit", 2];
			
			if (_number_v < 100 || {(_number_v > 999 && {_number_v < 1100})}) then {
				_vehicle addMPEventhandler ["MPKilled", {(_this select 0) call FUNC(MHQFunc)}];
			};
			
#ifdef __TT__
			if (_number_v < 100) then {
				_vehicle addMPEventhandler ["MPKilled", {if (isServer) then {_this call FUNC(checkveckillblufor)}}];
			} else {
				_vehicle addMPEventhandler ["MPKilled", {if (isServer) then {_this call FUNC(checkveckillopfor)}}];
			};
#endif
			_vehicle addMPEventhandler ["MPKilled", {if (isServer) then {_this call FUNC(fuelCheck)}}];
			
			_vec_a set [0, _vehicle];
			_vec_array set [_forEachIndex, _vec_a];
			_vehicle setVariable [QGVAR(OUT_OF_SPACE), -1];
			_vehicle setVariable [QGVAR(vec), _number_v, true];
			_vehicle setAmmoCargo 0;
			_vehicle setVariable [QGVAR(vec_islocked), _isitlocked];
			if (_isitlocked) then {_vehicle lock true};
			[QGVAR(n_v), _vehicle] call FUNC(NetCallEventToClients);
			
			if (__Trans(transportAmmo) > 0) then {
				_vehicle setAmmoCargo __TransCargo;
			} else {
				if (__Trans(transportRepair) > 0) then {
					_vehicle setRepairCargo __TransCargo;
				} else {
					if (__Trans(transportFuel) > 0) then {
						_vehicle setFuelCargo __TransCargo;
					};
				};
			};

			//TODO No camo nets in A3
			if (GVAR(with_base_camonet) == 0) then {
				if (isNull _camo) then {
					_camotype = switch (getNumber (configFile/"CfgVehicles"/typeOf _vehicle/"side")) do {
						case 1: {"CamoNet_OPFOR_big_F"}; // Blufor vec
						case 0: {"CamoNet_BLUFOR_big_F"};
					};
					_camo = createVehicle [_camotype, position _vehicle, [], 0, "NONE"];
					_camo setDir (direction _vehicle) + 180;
					_camo setPos position _vehicle;
					_vehicle setVariable [QGVAR(camonet), _camo];
				};
			};
		};
		sleep (8 + random 5);
	} forEach _vec_array;
};